package api;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Order;
import net.sf.json.JSONObject;
import refund.RefundService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import common.StringUtil;

import appmodel.JsonModel;
import service.OrderService;

@Controller
@RequestMapping("/CustomerOrder")
public class CustomerOrderController {
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject add(HttpServletRequest request, HttpServletResponse response, JsonModel jsonModel,
			@RequestParam(value = "createrName", required = false) String createrName,
			@RequestParam(value = "createrPhone", required = false) String createrPhone,
			@RequestParam(value = "createrCompany", required = false) String createrCompany,
			@RequestParam(value = "createrAddress", required = false) String createrAddress,
			@RequestParam(value = "repairContent", required = false) String repairContent,
			@RequestParam(value = "repairAddress", required = false) String repairAddress,
			@RequestParam(value = "repairBrands", required = false) String repairBrands,
			@RequestParam(value = "repairOrigin", required = false) String repairOrigin,
			@RequestParam(value = "repairNumber", required = false) String repairNumber,
			@RequestParam(value = "repairSystem", required = false) String repairSystem,
			@RequestParam(value = "breakDescription", required = false) String breakDescription,
			@RequestParam(value = "picShow", required = false) String picShow,
			@RequestParam(value = "remark", required = false) String remark,
			@RequestParam(value = "price", required = false) String price,
			@RequestParam(value = "type", required = false) String type,
			@RequestParam(value = "userId", required = false) long userId,
			@RequestParam(value = "reserveTime", required = false) String reserveTime,
			@RequestParam(value = "additionalPrice", required = false) String additionalPrice,
			@RequestParam(value = "remarkFault", required = false) String remarkFault) {

		response.addHeader("Access-Control-Allow-Origin", "*");
		response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type");
		response.addHeader("Access-Control-Max-Age", "7200");// 120 min

		Order order = new Order();
		order.setOrderid(String.valueOf(new Date().getTime()));
		order.setCreaterName(createrName);
		order.setCreaterPhone(createrPhone);
		order.setCreaterCompany(createrCompany);
		order.setCreaterAddress(createrAddress);
		order.setRepairContent(repairContent);
		order.setRepairAddress(repairAddress);
		order.setRepairBrands(repairBrands);
		order.setRepairOrigin(repairOrigin);
		order.setRepairNumber(repairNumber);
		order.setRepairSystem(repairSystem);
		order.setBreakDescription(breakDescription);
		order.setPicShow(picShow);
		order.setRemark(remark);
		order.setPrice(Double.valueOf(price));
		order.setType(type);
		order.setState(1);
		order.setCreate_user_id(userId);
		if (StringUtil.isEmpty(reserveTime)) {
			order.setReserveTime(new Date());
		} else {
			order.setReserveTime(StringUtil.StringToDate(reserveTime));
		}
		if(!StringUtil.isEmpty(additionalPrice)) {
			order.setAdditionalPrice(Double.valueOf(additionalPrice));
		}
		order.setRemarkFault(remarkFault);

		int addOrder = orderService.insert(order);

		if (addOrder > 0) {
			jsonModel.setSuccess(true);
			jsonModel.setMsg("添加成功");
			jsonModel.setjData("");
			jsonModel.setToken("");
		} else {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("查询失败");
			jsonModel.setjData("");
			jsonModel.setToken("");
		}

		return JSONObject.fromObject(jsonModel);
	}

	@RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject cancelOrder(HttpServletRequest request, HttpServletResponse response, JsonModel jsonModel,
			@RequestParam(value = "oId", required = false) String oId) {

		response.addHeader("Access-Control-Allow-Origin", "*");
		response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type");
		response.addHeader("Access-Control-Max-Age", "1800");// 30 min

//		Order order = new Order();
//		order.setoId(oId);
//		order.setState(7);
//		int cancelOrder = orderService.cancelOrder(order);
		
		Order order = orderService.getOrderDetail(oId);
		if(null != order) {
			if(order.getState() == 7) {//如果这条订单已经取消，提示"该订单已取消，请勿重复取消"
				jsonModel.setSuccess(false);
				jsonModel.setMsg("该订单已取消，请勿重复取消");
				jsonModel.setjData("");
				jsonModel.setToken("");
			}else if(order.getState() == 1){//如果是1的话，才可以取消订单
				Long userId = order.getCreate_user_id();//获取用户的id  //请求退款的接口，如果退款成功后
				double priceAll = order.getAdditionalPrice() + order.getPrice();
				long priceFen = (long) (priceAll * 100);
				
				RefundService refundService = new RefundService(userId, priceFen);
				refundService.loginServer(new refund.ReFundFinish() {
					@Override
					public void reFundFinish() {
						//请求取消成功后，修改
						Order order = new Order();
						order.setOrderid(oId);
						order.setState(7);
						int cancelOrder = orderService.cancelOrder(order);
						
						if (cancelOrder > 0) {
							jsonModel.setSuccess(true);
							jsonModel.setMsg("取消成功");
							jsonModel.setjData("");
							jsonModel.setToken("");
						} else {
							jsonModel.setSuccess(false);
							jsonModel.setMsg("取消失败");
							jsonModel.setjData("");
							jsonModel.setToken("");
						}
					}
				});
			}else {//其他的，不能点击
				jsonModel.setSuccess(false);
				jsonModel.setMsg("该状态不能取消订单");
				jsonModel.setjData("");
				jsonModel.setToken("");
			}
		}else{
			jsonModel.setSuccess(false);
			jsonModel.setMsg("该订单不存在");
			jsonModel.setjData("");
			jsonModel.setToken("");
		}
		return JSONObject.fromObject(jsonModel);
	}
}
